package com.mmall.controller;

import com.mmall.common.ServerResponse;
import com.mmall.dto.DeptLevelDto;
import com.mmall.param.DeptParm;
import com.mmall.service.ISysDeptService;
import com.mmall.service.ISysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author wangtengyu
 * @Create 2018-03-07-9:55
 */
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysTreeService iSysTreeService;

    /**
     * 界面
     * @return
     */
    @RequestMapping("/dept.page")
    public ModelAndView page(){
        return new ModelAndView("dept");
    }


    /**
     * 保存
     * @param deptParm
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public ServerResponse saveDept(DeptParm deptParm){
        iSysDeptService.save(deptParm);
        return ServerResponse.createBySuccess("新增部门成功");
    }

    /**
     * 更新
     * @param deptParm
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public ServerResponse updateDept(DeptParm deptParm){
        iSysDeptService.update(deptParm);
        return ServerResponse.createBySuccess("更新部门成功");
    }



    @RequestMapping("/delete.json")
    @ResponseBody
    public ServerResponse delete(@RequestParam(name = "id") int id){
        iSysDeptService.delete(id);
        return ServerResponse.createBySuccess("删除部门成功");
    }

    /**
     * 返回树形结构数据
     * @return
     */
    @RequestMapping("/tree.json")
    @ResponseBody
    public ServerResponse tree(){
        List<DeptLevelDto> dtoList=iSysTreeService.deptTree();
        return ServerResponse.createBySuccess(dtoList);
    }

}
